/*
 * @lc app=leetcode.cn id=65 lang=cpp
 *
 * [65] 有效数字
 */

// @lc code=start
#include <bits/stdc++.h>
using namespace std;
class Solution
{
    // 自己画个状态机
public:
    enum STATE
    {
        S0,
        S1,
        S2,
        S3,
        S4,
        S5,
        S6,
        S7,
        S8,
        END
    };
    bool isNumber(char c)
    {
        return c >= '0' && c <= '9';
    }
    bool isNumber(string s)
    {
        STATE state = S0;
        for (int i = 0; i < s.size(); i++)
        {
            switch (state)
            {
            case S0:
            {
                switch (s[i])
                {
                case '+':
                case '-':
                    state = S2;
                    break;
                case '.':
                    state = S1;
                    break;
                default:
                    if (isNumber(s[i]))
                    {
                        state = S3;
                    }
                    else
                    {
                        state = END;
                    }
                    break;
                }
                break;
            }
            case S1:
            {
                switch (s[i])
                {
                default:
                    if (isNumber(s[i]))
                    {
                        state = S5;
                    }
                    else
                    {
                        state = END;
                    }
                    break;
                }
                break;
            }
            case S2:
            {
                switch (s[i])
                {
                case '.':
                    state = S1;
                    break;

                default:
                    if (isNumber(s[i]))
                        state = S3;
                    else
                        state = END;
                    break;
                }
                break;
            }
            case S3:
            {
                switch (s[i])
                {
                case 'e':
                case 'E':
                    state = S6;
                    break;
                case '.':
                    state = S4;
                    break;
                default:
                    if (isNumber(s[i]))
                        state = S3;
                    else
                        state = END;
                    break;
                }
                break;
            }
            case S4:
            case S5:
            {
                switch (s[i])
                {
                case 'e':
                case 'E':
                    state = S6;
                    break;

                default:
                    if (isNumber(s[i]))
                        state = S4;
                    else
                        state = END;
                    break;
                    break;
                }
                break;
            }
            case S6:
            {
                switch (s[i])
                {
                case '-':
                case '+':
                    state = S8;
                    break;

                default:
                    if (isNumber(s[i]))
                        state = S7;
                    else
                        state = END;
                    break;
                }
                break;
            }
            case S7:
            {
                switch (s[i])
                {
                default:
                    if (isNumber(s[i]))
                        state = S7;
                    else
                        state = END;
                    break;
                }
                break;
            }
            case S8:
            {
                if (isNumber(s[i]))
                    state = S7;
                else
                    state = END;
                break;
            }
            default:
                return false;
                break;
            }
        }

        if (state == S3 || state == S4 || state == S5 || state == S7)
        {
            return true;
        }
        return false;
    }
};
// @lc code=end
